import {NB_AUTH_OPTIONS,getDeepFromObject} from "@nebular/auth";
import {Component, Inject} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router";
import {UserService} from "./user.service";

@Component({
  templateUrl: './login.component.html'
})
export class LoginComponent {
  showMessages: { error: boolean, success: boolean };
  errors: string[] = [];
  messages: string[] = [];
  submitted: boolean = false;
  user: { email: string, password: string, rememberMe: boolean } = {email: '', password: '', rememberMe: false};
  rememberMe: boolean;
  constructor(@Inject(NB_AUTH_OPTIONS) protected options = {}, private userService:UserService,private router: Router,private route:ActivatedRoute) {
    this.showMessages = this.getConfigValue('forms.login.showMessages');
    this.rememberMe = this.getConfigValue('forms.login.rememberMe');
    route.paramMap.subscribe(params=>{
      if(params.get('action')=='logout'){
        this.logout()
      }
    })
  }

  login() {
    this.userService.login(this.user.email,this.user.password).subscribe(user=>{
      this.router.navigateByUrl("/switch");
    });
  }

  getConfigValue(key: string): any {
    return getDeepFromObject(this.options, key, null);
  }

  logout(){
    this.userService.logout();
  }
}
